<template>
  <div id="app">
    <nav class="pixel-nav">
      <div class="nav-container">
        <div class="nav-brand">
          <h1 class="pixel-title">🎓 北交大教室查询</h1>
        </div>
        <div class="nav-links">
          <px-space>
            <router-link to="/chat">
              <px-link href="/chat" theme="primary">教室查询</px-link>
            </router-link>
            <px-link theme="info" @click="showSchedule = true">
              ⏰ 节次时间
            </px-link>
            <template v-if="systemStore.isAuthenticated">
              <px-link theme="success" @click="handleAlreadyLoggedIn">
                ✅ 已登录
              </px-link>
            </template>
            <template v-else>
              <router-link to="/login">
                <px-link href="/login" theme="success">登录</px-link>
              </router-link>
            </template>
          </px-space>
        </div>
      </div>
    </nav>

    <main class="main-content">
      <router-view />
    </main>

    <!-- 节次时间表弹窗 -->
    <ScheduleModal :show="showSchedule" @close="handleScheduleClose" />

    <!-- 提示消息 -->
    <div v-if="showTooltip" class="tooltip pixel-border" @click="showTooltip = false">
      <div class="tooltip-content">
        <span class="tooltip-icon">✅</span>
        <span class="tooltip-text">您已经登录系统了！可以直接使用教室查询功能。</span>
        <span class="tooltip-close">✕</span>
      </div>
    </div>

    <footer class="pixel-footer">
      <div class="footer-content">
        <p>© 2025 北京交通大学教室查询系统</p>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useSystemStore } from '@/stores/system'
import ScheduleModal from '@/components/ScheduleModal.vue'

const systemStore = useSystemStore()
const showTooltip = ref(false)
const showSchedule = ref(false)

// 处理已登录用户点击登录按钮
const handleAlreadyLoggedIn = () => {
  showTooltip.value = true
  // 3秒后自动关闭
  setTimeout(() => {
    showTooltip.value = false
  }, 3000)
}

// 处理节次时间表弹窗关闭
const handleScheduleClose = () => {
  console.log('父组件接收到关闭事件，showSchedule当前值:', showSchedule.value)
  showSchedule.value = false
  console.log('父组件设置showSchedule为false，新值:', showSchedule.value)
}

// 应用启动时获取系统状态
onMounted(async () => {
  try {
    await systemStore.fetchSystemStatus()
  } catch (error) {
    console.error('获取系统状态失败:', error)
  }
})
</script>

<style scoped>
.pixel-nav {
  background-color: white;
  border-bottom: 4px solid var(--pixel-primary);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.8);
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-brand .pixel-title {
  font-size: 16px;
  color: var(--pixel-primary);
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.3);
}

.main-content {
  flex: 1;
  min-height: calc(100vh - 200px);
  padding: 0 8px;
}

.pixel-footer {
  background-color: var(--pixel-dark);
  color: white;
  border-top: 3px solid var(--pixel-primary);
  box-shadow: 0 -3px 0 rgba(0, 0, 0, 0.8);
  margin-top: auto;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 8px;
  text-align: center;
  font-size: 10px;
}

.footer-content p {
  margin: 0;
}

/* 手机端优化 */
@media (max-width: 768px) {
  .nav-container {
    padding: 12px 16px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .nav-brand {
    flex: 1;
    min-width: 100%;
  }

  .nav-brand .pixel-title {
    font-size: 14px;
    text-align: center;
    margin: 0;
  }

  .nav-links {
    min-width: 100%;
    text-align: center;
  }

  .nav-links .px-space {
    justify-content: center;
  }

  .main-content {
    padding: 0 4px;
    min-height: calc(100vh - 180px);
  }

  .footer-content {
    padding: 6px 8px;
  }
}

/* 提示框样式 */
.tooltip {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  background: var(--pixel-success);
  color: white;
  padding: 12px 16px;
  box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.8);
  cursor: pointer;
  animation: slideDown 0.3s ease-out;
}

.tooltip-content {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: bold;
}

.tooltip-icon {
  font-size: 16px;
  flex-shrink: 0;
}

.tooltip-text {
  flex: 1;
  line-height: 1.4;
}

.tooltip-close {
  font-size: 14px;
  opacity: 0.8;
  margin-left: 4px;
  flex-shrink: 0;
}

.tooltip-close:hover {
  opacity: 1;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* 已登录状态样式 */
.nav-links .px-link[theme="success"] {
  cursor: default;
  position: relative;
}

.nav-links .px-link[theme="success"]:hover {
  transform: scale(1.05);
}

/* 手机端提示框优化 */
@media (max-width: 768px) {
  .tooltip {
    top: 100px;
    left: 16px;
    right: 16px;
    transform: none;
    padding: 10px 12px;
  }

  .tooltip-content {
    font-size: 11px;
  }
}

/* 超小屏幕优化 */
@media (max-width: 480px) {
  .tooltip {
    top: 90px;
    left: 8px;
    right: 8px;
    padding: 8px 10px;
  }

  .tooltip-content {
    font-size: 10px;
    gap: 6px;
  }

  .tooltip-icon {
    font-size: 14px;
  }

  .tooltip-close {
    font-size: 12px;
  }

  .nav-container {
    padding: 8px 12px;
  }

  .nav-brand .pixel-title {
    font-size: 12px;
  }

  .main-content {
    padding: 0;
  }
}
</style>